﻿using Konveyer.Core.Attributes;
using Konveyer.Core.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Konveyer.Core.Tools
{
    /// <summary>
    /// Creates a report from Widgets, to aid documentation (and therefore re use)
    /// </summary>
    public class HtmlWidgetReporter : IWidgetReporter
    {
        public string CreateReport(params Type[] types)
        {
            var rtn = new StringBuilder();
            rtn.Append("<table>");
            foreach (var type in types)
            {
                var attribs = GetBeltAttributes(type);
                attribs.ForEach(x =>
                {

                    rtn.Append("<tr><td>")
                        .Append(type.Name)
                        .Append("</td><td>")
                        .Append(x.Step.ToString())
                        .Append("</td><td>")
                        .Append(x.Widget.Name)
                        .Append("</td><td>")
                        .Append(x.Description)
                        .Append("</td></tr>");
                });
            }


            rtn.Append("</table>");


            return rtn.ToString();


        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        private List<BeltAttribute> GetBeltAttributes(Type type)
        {
            var rtn = new List<BeltAttribute>();

            var tmp = type.GetCustomAttributes(typeof(BeltAttribute), true);
            if (tmp == null)
                return rtn;

            var attributes = tmp.Cast<BeltAttribute>();
            rtn.AddRange(attributes);
            rtn = rtn.OrderBy(x => x.Step).ToList();
            return rtn;
        }
    }
}
